package edu.osu.cse.c651.cryptochess;

import java.util.ArrayList;

/**
 * The Pawn
 * 
 * @author Team Pawned
 * 
 */
public class Pawn implements Piece {

	/**
	 * The Color of this Pawn
	 */
	private Color color;

	/**
	 * The number of times this Pawn has moved
	 */
	private int moveCount;

	/**
	 * Constructor
	 * 
	 * @param color
	 *            the Color of this Pawn
	 */
	public Pawn(Color color) {
		this.color = color;
		moveCount = 0;
	}

	/**
	 * 
	 */
	public boolean legalMove(int x1, int y1, int x2, int y2) {
		if (this.color == Color.WHITE) {
			if ((y2 == y1) && (x2 == x1 + 1)) {
				return true;
			}
		} else {
			if ((y2 == y1) && (x2 == x1 - 1)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 */
	public Color color() {
		return color;
	}

	/**
	 * 
	 */
	public PieceName name() {
		return PieceName.Pawn;
	}

	/**
	 * 
	 */
	public void movesPlusOne() {
		moveCount += 1;
	}

	/**
	 * 
	 */
	public int moveCount() {
		return moveCount;
	}

	/**
	 * 
	 */
	public ArrayList<Coordinate> possibleMoves(ChessBoard board, int x, int y) {
		ArrayList<Coordinate> moves = new ArrayList<Coordinate>();

		// Black pawns can only move down
		if (this.color == Color.BLACK) {
			// Checks the single hop down
			if (x - 1 >= 0 && board.getPiece(x - 1, y) == null) {
				Coordinate move = new Coordinate(x - 1, y);
				moves.add(move);
			}

			// Checks double hop
			if (x == 6 && board.getPiece(x - 2, y) == null) {
				Coordinate move = new Coordinate(x - 2, y);
				moves.add(move);
			}

			// Checks diagonal attacks
			if ((x - 1 >= 0 && y + 1 < 8)
					&& board.getPiece(x - 1, y + 1) != null
					&& board.getPiece(x - 1, y + 1).color() == Color.WHITE) {
				Coordinate move = new Coordinate(x - 1, y + 1);
				moves.add(move);
			}
			if ((x - 1 >= 0 && y - 1 >= 0)
					&& board.getPiece(x - 1, y - 1) != null
					&& board.getPiece(x - 1, y - 1).color() == Color.WHITE) {
				Coordinate move = new Coordinate(x - 1, y - 1);
				moves.add(move);
			}
		}

		// White pawns can only move up
		else {
			// Checks the single hop up
			if (x + 1 < 8 && board.getPiece(x + 1, y) == null) {
				Coordinate move = new Coordinate(x + 1, y);
				moves.add(move);
			}

			// Checks double hop
			if (x == 1 && board.getPiece(x + 2, y) == null) {
				Coordinate move = new Coordinate(x + 2, y);
				moves.add(move);
			}

			// Checks diagonal attacks
			if ((x + 1 < 8 && y + 1 < 8)
					&& board.getPiece(x + 1, y + 1) != null
					&& board.getPiece(x + 1, y + 1).color() == Color.BLACK) {
				Coordinate move = new Coordinate(x + 1, y + 1);
				moves.add(move);
			}
			if ((x + 1 < 8 && y - 1 >= 0)
					&& board.getPiece(x + 1, y - 1) != null
					&& board.getPiece(x + 1, y - 1).color() == Color.BLACK) {
				Coordinate move = new Coordinate(x + 1, y - 1);
				moves.add(move);
			}
		}

		return moves;
	}
}
